package com.accesshollywood.components;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.EnumSet;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

public class BasicPopupMenu extends JPopupMenu {
	public enum POPUP_ITEMS {
		ADD,
		EDIT,
		COPY,
		DELETE,
		READ_ALOUD,
		SPELL_ALOUD,
		OPEN_FOLDER,
		CLEAR_FOLDER
	}

	public EnumSet<BasicPopupMenu.POPUP_ITEMS>	popupItems;
	protected HasPopupMenu						parentComponent;
	public int X,Y;

	public BasicPopupMenu(HasPopupMenu parent) {
		super();
		this.parentComponent = parent;
		parent.addMouseListener(new PopupListener());
	}
	
	public HasPopupMenu getParentComponent(){
		return parentComponent;
	}
	
	public JMenu addMenu(String label){
		JMenu newMenu = new JMenu(label);
		add(newMenu);
		return newMenu;
	}

	public JMenuItem addMenuItem(String label, ActionListener action) {
		JMenuItem newMenuItem = new JMenuItem(label);
		newMenuItem.addActionListener(action);
		add(newMenuItem);
		return newMenuItem;
	}

	public JMenuItem addMenuItem(String label, JMenu parent, ActionListener action) {
		JMenuItem newMenuItem = new JMenuItem(label);
		newMenuItem.addActionListener(action);
		parent.add(newMenuItem);
		return newMenuItem;
	}

	public class PopupListener extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			maybeShowPopup(e);
		}

		public void mouseReleased(MouseEvent e) {
			maybeShowPopup(e);

		}

		public void maybeShowPopup(MouseEvent e) {
			if (e.isPopupTrigger() || e.getButton()==MouseEvent.BUTTON3) {
				Point p = e.getPoint();
				// int rowNumber = rowAtPoint(p);
				// getSelectionModel().setSelectionInterval(rowNumber, rowNumber);
				Component c = e.getComponent();
				X = e.getX();
				Y = e.getY();
				show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}
